﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wq.Extensions;
using Wq.Extensions.Repository;
using Wq.ManagerService.Entities;
using Wq.ManagerService.Models;
using Wq.Model;
namespace Wq.ManagerService
{
    /// <summary>
    /// 图片分类
    /// </summary>
    public class CategoryService : SqlSugarBaseRepository<CategoryInfo>, IAddSingleton
    {

        private readonly RedisBaseRepository redisRepository;
        public CategoryService(RedisBaseRepository redisRepository)
        {
            this.redisRepository = redisRepository;
        }


        /// <summary>
        /// 分类
        /// </summary>
        /// <returns></returns>
        public List<CategoryEditor> List(FileType type) => redisRepository.Get(SysCacheKey.FileCategory((int)type), () => {

            var list = new List<CategoryEditor>();
            list.Add(new CategoryEditor() { Id = 0, Name = "未分组", FileCount = Context.Queryable<Wq.ManagerService.Entities.FileInfo>().Count(p => p.CategoryId == 0) });
            list.AddRange(Context.Queryable<CategoryInfo>().Where(p => p.Type == (int)type).Select<CategoryEditor>().ToList());
            var dictionary = Context.Queryable<Entities.FileInfo>().Where(p => p.Type == (int)type).GroupBy(p => p.CategoryId).ToDictionary(p => p.CategoryId, p => SqlFunc.AggregateCount(p.CategoryId));
            list.ForEach(item => {
                if (dictionary.ContainsKey(item.Id.ToString()))
                    item.FileCount = int.Parse(dictionary[item.Id.ToString()].ToString());
                else
                    item.FileCount = 0;
            });
            return list;
        }).Result;

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="category"></param>
        public void Save(CategoryEditor category)
        {
            RemoveCache(category.Type);
            Context.Updateable<CategoryInfo>().SetColumns(p => p.Name == category.Name).Where(p => p.Id == category.Id).ExecuteCommand();
        }

        /// <summary>
        /// 更新分类名称
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        public void SetName(long id, string name)
        {
            Context.Updateable<CategoryInfo>().SetColumns(p => p.Name == name).Where(p => p.Id == id).ExecuteCommand();
            var info = Context.Queryable<CategoryInfo>().First(p => p.Id == id);
            RemoveCache((FileType)info.Type);
        }

        public void Remove(long id)
        {
            Context.Ado.UseTran(() => {
                var info = Context.Queryable<CategoryInfo>().First(p => p.Id == id);
                Context.Updateable<Entities.FileInfo>().SetColumns(p => p.CategoryId == 0).Where(p => p.CategoryId == id).ExecuteCommand();
                Context.Deleteable<CategoryInfo>(p => p.Id == id).ExecuteCommand();
                RemoveCache((FileType)info.Type);
            });
        }

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="type"></param>
        public void RemoveCache(FileType type) => redisRepository.Remove(SysCacheKey.FileCategory((int)type));

    }

}
